package com.example.shopping.controller;

import com.example.shopping.pojo.Specification;
import com.example.shopping.pojo.SpecificationOption;
import com.example.shopping.service.SpecificationOptionSerivce;
import com.example.shopping.service.SpecificationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/*
Create by 苍狼 于 2020/1/16
站在巅峰看世界，
落在深渊思人生。
*/
@CrossOrigin
@Controller
public class SpecificationController {
    private int copyId;
    @Autowired(required = false)
    private SpecificationService specificationService;
    @Autowired(required = false)
    private SpecificationOptionSerivce specificationOptionSerivce;

    @RequestMapping("/selectSpecificationList")
    public String selectSpecificationList(HttpServletRequest request) {
        List<Specification> specifications = specificationService.selectSpecification();
        request.getSession().setAttribute("specifications", specifications);
        return "specification";
    }

    @RequestMapping("/insertSpecification")
    public String insertSpecification(HttpServletRequest request, Specification specification, SpecificationOption specificationOption) {
        //得到关联主键
        int s = specificationService.insertSpecification(specification);
        //得到主键回填
        copyId = specification.getCopyId();
        //设置给规格选项表的关联ID 进行增加操作
        specificationOption.setSpecId(copyId);
        int i = specificationOptionSerivce.insertSpecificationOption(specificationOption);
        if (i > 0 && s > 0) {
            return "redirect:/selectSpecificationList";
        }
        return "redirect:insertSpecification";
    }

    @RequestMapping("/deleteSpecification")
    public String deleteSpecification(HttpServletRequest request, int id) {
        //得到此要删除的全部信息，清切查询出关联ID，得到之后，进行同时删除
        Specification specification = specificationService.selectByOneSpecification(id);
        //将查询到的主键得到
        copyId = specification.getCopyId();
        //删除规格表
        int i = specificationService.deleteSpecification(id);
        //根据主键删除规格明细表数据
        specificationOptionSerivce.deleteSpecificationById(copyId);
        if (i > 0) {
            return "redirect:/selectSpecificationList";
        }
        return "redirect:deleteSpecification";
    }

    @RequestMapping("/quertByLikespecName")
    public String quertByLikespecName(HttpServletRequest request, String specName) {
        //得到此要删除的全部信息，清切查询出关联ID，得到之后，进行同时删除
        List<Specification> specifications = specificationService.quertByLikespecName(specName);
        request.getSession().setAttribute("specifications", specifications);
        return "quertByLikespecName";
    }

    //修改方法
    @RequestMapping("/updateSpecificationById")
    public String updateSpecificationById(int id, HttpServletRequest request, Specification specification, SpecificationOption specificationOption) {
        //得到此要删除的全部信息，清切查询出关联ID，得到之后，进行同时删除
        //查询此ID的信息
        Specification sp = specificationService.selectByOneSpecification(id);
        //通过查询的对象得到
        copyId = specification.getCopyId();
        int i = specificationService.updateSpecificationById(id);
        //得到主键
        //查询关联主键的规格详细信息
        SpecificationOption spo = specificationOptionSerivce.selectListSpecificationOptionByCopyId(copyId);
        //将关联规格的详细信息查询到，并且将ID传入数组中，
        int[] ints = {spo.getId()};
        for (int j = 0; j <= ints.length; j++) {
            //通过遍历数组中的id进行依次新增
            specificationOptionSerivce.updateSpecificationOptionById(new SpecificationOption(j, specificationOption.getOptionName(), copyId, specificationOption.getOrder()));

        }
        return "redirect:selectSpecificationList";
    }


}
